import {
  AccountListGetResultModel,
  AccountParams,
  DeptListGetResultModel,
  DeptListItem,
  MenuListGetResultModel,
  MenuParams,
  OldMenuParams,
  RoleListGetResultModel,
  RolePageListGetResultModel,
  RolePageParams,
  RoleParams,
} from './model/systemModel';
import {defHttp} from '/@/utils/http/axios';
import {listToTree} from "/@/utils/compUtils";

enum Api {
  AccountList = '/system/getAccountList',
  IsAccountExist = '/system/accountExist',
  DeptList = '/system/getDeptList',
  setRoleStatus = '/system/setRoleStatus',
  MenuList = '/menu/all',
  MenuAdd = '/menu/add',
  MenuUpdate = '/menu/update',
  MenuRemove = '/menu/remove',
  RolePageList = '/system/getRoleListByPage',
  GetAllRoleList = '/system/getAllRoleList',
}

export const getAccountList = (params: AccountParams) =>
  defHttp.get<AccountListGetResultModel>({url: Api.AccountList, params});

export const getDeptList = (params?: DeptListItem) =>
  defHttp.get<DeptListGetResultModel>({url: Api.DeptList, params});

export interface MenuListItem {
  id: string;
  orderNo: string;
  createTime: string;
  status: number;
  icon: string;
  component: string;
  permission: string;
}

export const getMenuList = (params?: MenuParams) =>
  defHttp.get<MenuListGetResultModel>({url: Api.MenuList, params}).then((data) => {
    const opt = {primaryKey: 'menuId', title: 'menuName'};
    const newData = listToTree(data, opt, '0');
    console.info('newData', newData)
    return newData
  });
export const menuAdd = (params?: OldMenuParams) =>
  defHttp.post<OldMenuParams>({url: Api.MenuAdd, params});

export const menuUpdate = (params?: OldMenuParams) =>
  defHttp.post<OldMenuParams>({url: Api.MenuUpdate, params});

export const getRoleListByPage = (params?: RolePageParams) =>
  defHttp.get<RolePageListGetResultModel>({url: Api.RolePageList, params});

export const getAllRoleList = (params?: RoleParams) =>
  defHttp.get<RoleListGetResultModel>({url: Api.GetAllRoleList, params});

export const setRoleStatus = (id: number, status: string) =>
  defHttp.post({url: Api.setRoleStatus, params: {id, status}});

export const isAccountExist = (account: string) =>
  defHttp.post({url: Api.IsAccountExist, params: {account}}, {errorMessageMode: 'none'});
